﻿/*
创 建 人     ： 陈 锐
创 建 时 间  ： 2012.02.10
*/
Daan = window.Daan || {};
Daan.Map = Daan.Map || {};
Daan.Map.Marker = function () {

};
Daan.Map.Search = function (map, option) {
    var setting = $.extend({
        key: "社区服务",
        city: "北京市",
        getAllResult: true,
        pageSize: 9999,
        forceLocal: true,
        itemGot: function () { }
    }, option);
    var resultInfo = {
        count: 0,
        page: 0,
        index: 0,
        size: setting.pageSize
    };
    var searching = true;
    var started = false;
    var local = new BMap.LocalSearch(setting.city, {
        renderOptions: {},
        autoViewport: false,
        selectFirstResult: false,
        pageCapacity: setting.pageSize,
        onSearchComplete: function (results) {
            if (local.getStatus() == BMAP_STATUS_SUCCESS) {
                // 判断状态是否正确  
                resultInfo.count = results.getNumPois();
                resultInfo.page = results.getNumPages();
                resultInfo.index = results.getPageIndex();
                for (var i = 0, c = results.getCurrentNumPois(); i < c; i++) {
                    var pos = results.getPoi(i);
                    if (typeof setting.itemGot == "function") {
                        setting.itemGot(map, pos);
                    }
                }
                if (searching && setting.getAllResult && resultInfo.index < resultInfo.page - 1) {
                    local.gotoPage(resultInfo.index + 1);
                }
            }
        }
    });

    function getResultInfo() {
        return resultInfo;
    };

    function getCity() {
        return setting.city;
    }

    function search() {
        if (!searching) {
            searching = true;
            local.gotoPage(resultInfo.index + 1);
        }
        if (!started) {
            started = true;
            local.search(setting.key);
        }
    };

    function stop() {
        searching = false;
    };

    return { getResultInfo: getResultInfo, search: search, stop: stop, getCity: getCity };
};